﻿package game.ui {
	import flash.display.DisplayObject;
	import flash.utils.setInterval;
	import flash.utils.clearInterval;
	import flash.display.MovieClip;
	import game.objects.GameObject;
	import game.debug.Debug;
	
	public class Slider extends GameObject {
		
		public static const VERTICAL:String		= 'v';
		public static const HORIZONTAL:String	= 'h';
		
		private var min_indent:Number	= 3;
		private var current:int = 0;
		private var _width:Number;
		private var _height:Number;
		private var line:String = HORIZONTAL;
		private var interval;
		private var test:Boolean = false;
		
		private var container:MovieClip = new MovieClip;
		private var maska:MovieClip = new MovieClip;
		
		public function Slider():void {
			addChild(container);
			addChild(maska);
			
			container.mask = maska;
			
			//проверяем дебаг ли
			for each (var d:String in config.debug) {
				if (d==Debug.DYNAMIC_OBJECTS) {
					test = true;
					break;
				}
			}
			
		}
		
		public function clear():void {
			while (container.numChildren) container.removeChildAt(0);
			current = 0;
			
			update();
		}
		
		public function add(object:DisplayObject):void {
			container.addChild(object);
			
			update();
		}
		
		public function set type(value:String):void {
			line = value;
			update();
		}
		
		public function update():void {
			this.alpha = 0;
			clearInterval(interval);
			interval = setInterval (resize, config.updateDynamic*1000);
		}
		
		public override function set width(value:Number):void {
			_width = value;
			
			update();
		}
		
		public override function set height(value:Number):void {
			_height = value;
			
			update();
		}
		
		public function step(value:int):void {
			current += value;
			
			update();
		}
		
		public override function get width():Number {
			return (line==HORIZONTAL) ? ((_width) ? _width : 0) : (container.width);
		}
		
		public override function get height():Number {
			return (line==VERTICAL) ? ((_height) ? _height : 0) : (container.height);
		}
		
		public function set indent(value:Number):void {
			min_indent = value;
		}
		
		private function resize():void {
			clearInterval(interval);
			
			if (container.numChildren==0) return;
			
			var l:int = length;
			
			if (l==0) return;
			
			//проверяем текущую позицию
			if (count>l) {
				if (current<0) current = 0;
				else if (current>count-l) current = count - l;
			} else {
				//если элементов меньше чем может влезть то всегда current = 0;
				current = 0;
				l = count;
			}
			
			//выход из прозрачности
			if (this.alpha!=1)
				animate ('alpha', this.alpha, 1, 0.1);
			
			if ((!_width && line==HORIZONTAL) || (!_height && line==VERTICAL)) return;
			
			//устанавливаем все элементы в 0
			for (var ichild:int = 0; ichild<container.numChildren; ichild++) {
				container.getChildAt(ichild).x = 0;
				container.getChildAt(ichild).y = 0;
			}
			container.x = 0;
			container.y = 0;
			
			//рисуем дебаг
			if (test) {
				with ( this.graphics ) {
					clear();
					lineStyle(1, 0xff0000);
					drawRect(0, 0, width, height);
					endFill();
				}
			}
			
			//рисуем маску
			with ( maska.graphics ) {
				clear();
				beginFill(0xff0000, 1);
				drawRect(0, 0, width, height);
				endFill();
			}
			
			//вычисляем точный отступ
			var p:Number = getIndent(l);
			
			var el:DisplayObject = container.getChildAt(0) as DisplayObject;
			//раставляем элементы в контейнере
			for (ichild = 0; ichild<container.numChildren; ichild++) {
				container.getChildAt(ichild)[(line==HORIZONTAL) ? 'x' : 'y'] = ichild*(el[attr]+p);
			}
			
			//устанавливаем контейнер
			container[(line==HORIZONTAL) ? 'x' : 'y'] = - current*(el[attr]+p);
			
		}
		
		public function get count():int { return container.numChildren; }
		
		//возвращает промежуток между слайдами
		private function getIndent(c:int):Number {
			var el:DisplayObject = container.getChildAt(0) as DisplayObject;
			var i:Number = (this[attr]-(c*el[attr]))/(c-1);
			return i;
		}
		
		//предположительно максимальное кол-во слайдов
		public function get length():int {
			var l:Number = 0;
			var c:int = 0;
			if (container.numChildren==0) return l;
			var el:DisplayObject = container.getChildAt(0) as DisplayObject;
			while ( l < this[attr]) {
				l += el[attr] + min_indent;
				c++;
			}
			l-=min_indent;
			if (l>this[attr]) {
				c--;
			}
			return c;
		}
		
		private function get attr():String { return (line==HORIZONTAL) ? 'width' : 'height'; }
		
	}
}